//
//  SelectPayStyleView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/5.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum SelectPayStyle: Int {
    case wechat
    /// 支付宝医保小程序
//    case medicalInsuranceFormAlipay
    
    var icon: UIImage? {
        switch self {
        case .wechat: return UIImage(named: "wechatpay")
//        case .medicalInsuranceFormAlipay: return UIImage(named: "alipay")
        }
    }
}

class SelectPayStyleView: UIView {
    
    var handlerClosure: ((_ style: SelectPayStyle) -> Void)?
    var cancelClosure: (() -> Void)?
    
    private var style: SelectPayStyle = .wechat
    
    var dataSource: [SelectPayStyle] = [.wechat] //[.wechat, .medicalInsuranceFormAlipay]
    
    @IBOutlet private weak var topMaskView: UIView! {
        didSet {
            topMaskView.alpha = 0.0
            let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
            topMaskView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var contentView: UIView! {
        didSet {
            contentView.layer.cornerRadius = 16
            contentView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var contentViewHConstraint: NSLayoutConstraint! {
        didSet {
            contentViewHConstraint.constant = 0
        }
    }
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var costLabel: UILabel!
    
    @IBOutlet private weak var payBtn: UIButton! {
        didSet {
            payBtn.layer.cornerRadius = payBtn.height / 2
            payBtn.layer.masksToBounds = true
            payBtn.setBackgroundImage(UIImage(color: .mainColorLight), for: .disabled)
            payBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
            payBtn.setBackgroundImage(UIImage(color: .mainColorGray), for: .highlighted)
        }
    }

    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.delegate = self
            tableView.dataSource = self
            tableView.rowHeight = 44
            tableView.estimatedRowHeight = UITableView.automaticDimension
            tableView.backgroundColor = .white
            tableView.tableFooterView = UIView()
            tableView.register(SelectPayStyleCell.nibForCell(),
                               forCellReuseIdentifier: SelectPayStyleCell.reuseIdentifier())
        }
    }
    
    @IBAction private func closeAction(_ sender: UIButton) {
        dismiss()
        cancelClosure?()
    }
    
    @IBAction private func payAction(_ sender: UIButton) {
        dismiss()
        handlerClosure?(style)
    }
    
    func configure(cost: String, time: String) {
        costLabel.text = "￥\(cost)"
        let attrStr = NSMutableAttributedString(string: "请在\(time)分钟内支付")
        let attributes = [NSAttributedString.Key.foregroundColor: UIColor(hex: "#E99F0A")]
        attrStr.setAttributes(attributes, range: NSRange(location: 2, length: time.count + 2))
        titleLabel.attributedText = attrStr
    }
    
    func show(in view: UIView? = nil) {
        let superview = view == nil ? mainWindow : view!
        superview.addSubview(self)
        
        self.frame = superview.bounds
        contentViewHConstraint.constant = 316
        
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
            self.topMaskView.alpha = 1
            self.layoutIfNeeded()
        }, completion: nil)
    }
    
    @objc private func dismiss() {
        contentViewHConstraint.constant = 0
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
            self.topMaskView.alpha = 0
            self.layoutIfNeeded()
        }) { _ in
            self.removeFromSuperview()
        }
    }
}

extension SelectPayStyleView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SelectPayStyleCell.reuseIdentifier(),
                                                 for: indexPath) as! SelectPayStyleCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == style.rawValue {
            cell.setSelected(true, animated: false)
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        style = dataSource[indexPath.row]
    }
}

